package com.rzico.sbl.ui.report.depot

import android.graphics.Color
import com.github.mikephil.charting.data.BarEntry
import com.github.mikephil.charting.data.Entry
import com.github.mikephil.charting.formatter.IAxisValueFormatter
import com.github.mikephil.extend.CustomValueFormatter
import com.rzico.sbl.R
import com.rzico.sbl.databinding.ActivityReportDepotBinding
import com.rzico.sbl.model.DepotBarrelDate
import com.rzico.sbl.model.ManagerDate
import com.rzico.sbl.other.*
import com.rzico.sbl.viewmodel.ReportDepotViewModel
import com.xinnuo.common.extend.get
import com.xinnuo.common.extend.oneClick
import com.xinnuo.common.extend.startActivityEx
import com.xinnuo.common.helper.doubleValue
import com.xinnuo.common.helper.floatValue
import com.xinnuo.common.helper.intValue
import com.xinnuo.common.helper.toFormat
import com.xinnuo.common_ui.base.BaseActivity
import com.xinnuo.common_ui.base.addItems

class ReportDepotActivity : BaseActivity(R.layout.activity_report_depot) {

    private val mBinding by lazy { ActivityReportDepotBinding.bind(getRootView()) }

    // 桶数/订单
    private val mTotalList = ArrayList<DepotBarrelDate>()
    private var mTotalBeginDate = getBeforeDate(6)
    private var mTotalEndDate = getTodayDate()

    // 异常订单
    private val mErrorList = ArrayList<ManagerDate>()
    private var mErrorBeginDate = getBeforeDate(6)
    private var mErrorEndDate = getTodayDate()

    override fun getViewModel() = get<ReportDepotViewModel>()

    override fun initData() {
        initTitle(title = "库管统计", name = "查看明细")

        initLayout()

        getTotalData()
    }

    private fun initLayout() {
        mBinding.also {
            it.depotBarrel.apply {
                initChart(
                    isCenterLabel = true,
                    isXGridLine = true,
                    isYGridLine = false,
                    xAxisMinimum = 0f,
                    yAxisMinimum = 0f,
                    xSpaceEntry = 50f
                )

                // 格式化x轴文本显示
                xAxis.valueFormatter = IAxisValueFormatter { value, _ ->
                    return@IAxisValueFormatter if (value.toInt() !in mTotalList.indices) ""
                    else
                        mTotalList[value.toInt()]
                            .date
                            .toFormat("yyyy-MM-dd", "M.d")
                }
            }

            it.depotOrder.apply {
                initChart(
                    isCenterLabel = true,
                    isXGridLine = true,
                    isYGridLine = false,
                    xAxisMinimum = 0f,
                    yAxisMinimum = 0f,
                    xSpaceEntry = 50f
                )

                // 格式化x轴文本显示
                xAxis.valueFormatter = IAxisValueFormatter { value, _ ->
                    return@IAxisValueFormatter if (value.toInt() !in mTotalList.indices) ""
                    else
                        mTotalList[value.toInt()]
                            .date
                            .toFormat("yyyy-MM-dd", "M.d")
                }
            }

            it.depotError.apply {
                initChart(yAxisMinimum = 0f)

                // 格式化x轴文本显示
                xAxis.valueFormatter = IAxisValueFormatter { value, _ ->
                    return@IAxisValueFormatter if (value.toInt() !in mErrorList.indices) ""
                    else mErrorList[value.toInt()].date.toFormat("yyyy-MM-dd", "M.d")
                }
            }
        }

        tvRight?.oneClick { startActivityEx<ReportDepotListActivity>() }
    }

    override fun initListener() {
        mBinding.apply {
            depotAllTime.addTimeListener { start, end, _ ->
                mTotalBeginDate = start
                mTotalEndDate = end

                getViewModel().cancelRequest()
                getTotalData()
            }

            depotErrorTime.addTimeListener { start, end, _ ->
                mErrorBeginDate = start
                mErrorEndDate = end

                getViewModel().cancelRequest()
                getErrorData()
            }
        }
    }

    /**
     * 设置总数据图表数据
     */
    private fun setTotalChart() {
        mBinding.apply {
            depotBarrel.apply {
                if (mTotalList.none { it.d_b.intValue > 0 }
                    && mTotalList.none { it.c_b.intValue > 0 }) clear()
                else {
                    setGroupData(
                        entries = arrayListOf(
                            mTotalList.mapIndexed { index, data ->
                                BarEntry(
                                    index.toFloat(),
                                    data.d_b.floatValue
                                )
                            },
                            mTotalList.mapIndexed { index, data ->
                                BarEntry(
                                    index.toFloat(),
                                    data.c_b.floatValue
                                )
                            }
                        ),
                        colorList = getViewModel().barrelAllColor(),
                        labelList = listOf("出库", "入库"),
                        xFromValue = 0f,
                        formatter = CustomValueFormatter()
                    )
                }

                animateY(500)
            }

            depotOrder.apply {
                if (mTotalList.none { it.d_o.intValue > 0 }
                    && mTotalList.none { it.c_o.intValue > 0 }) clear()
                else {
                    setGroupData(
                        entries = arrayListOf(
                            mTotalList.mapIndexed { index, data ->
                                BarEntry(
                                    index.toFloat(),
                                    data.d_o.floatValue
                                )
                            },
                            mTotalList.mapIndexed { index, data ->
                                BarEntry(
                                    index.toFloat(),
                                    data.c_o.floatValue
                                )
                            }
                        ),
                        colorList = getViewModel().color5(),
                        labelList = listOf("出库", "入库"),
                        xFromValue = 0f,
                        formatter = CustomValueFormatter()
                    )
                }

                animateY(500)
            }
        }
    }

    /**
     * 设置异常订单曲线图数据
     */
    private fun setErrorChart() {
        mBinding
            .depotError
            .apply {
                if (mErrorList.none { it.quantity.doubleValue > 0 }) clear()
                else {
                    setSingleData(
                        entries = mErrorList.mapIndexed { index, data ->
                            Entry(
                                index.toFloat(),
                                data.quantity.floatValue
                            )
                        },
                        lineColor = Color.parseColor("#FF0000"),
                        labelColor = Color.parseColor("#FF0000"),
                        highColor = Color.parseColor("#FF0000"),
                        drawableFill = R.drawable.fade_ff0000_to_trans,
                        valueColor = Color.parseColor("#FF0000"),
                        formatter = CustomValueFormatter()
                    )
                }

                animateY(500)
            }
    }

    /**
     * 获取桶数/订单柱状图数据
     */
    private fun getTotalData() {
        getViewModel()
            .barrelTotal(
                beginDate = mTotalBeginDate,
                endDate = mTotalEndDate,
                onSuccess = {
                    mTotalList.clear()
                    mTotalList.addItems(it)

                    setTotalChart()
                }
            )
    }

    /**
     * 获取异常订单
     */
    @Suppress("unused")
    private fun getErrorData() {
        getViewModel()
            .errorList(
                beginDate = mErrorBeginDate,
                endDate = mErrorEndDate,
                onSuccess = {
                    mErrorList.clear()
                    mErrorList.addItems(it)

                    setErrorChart()
                }
            )
    }

}